package Multithreading.Exercise003;

/**
 * ClassName:MyThread
 * Package:Multithreading.Exercise003
 * Description:
 *
 * @Author:ZhangXin
 * @Create2023/3/5-17:34
 * @Version:v1.0
 */
public class MyRunnable implements Runnable {
    int ticket = 0;

    @Override
    public void run() {
        //1.循环
        //2.同步代码块(同步方法)
        //3.判断共享数据是否到了末尾,如果到了末尾
        //4.判断共享数据是否到了末尾,如果没有到末尾
        while (true) {
            //2.同步代码块(同步方法)
            if (methon()) break;

        }
    }

    //this
    private synchronized boolean methon() {
        //3.判断共享数据是否到了末尾,如果到了末尾
        if (ticket == 100) {

            return true;

        } else {
            //4.判断共享数据是否到了末尾,如果没有到末尾
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ticket++;
            String name = Thread.currentThread().getName();
            System.out.println(name + "在卖第" + ticket + "张票!!");
        }
        return false;
    }
}
